<?php
/**
 * 我的商城
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('ByShopWWI') or exit('Access Invalid!');
class member_indexControl extends mobileMemberControl {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 我的商城
	 */
	public function indexOp() {
		$member_info = array();
		$member_info = $this -> getMemberAndGradeInfo(true);
		$member_info['user_name'] = $this -> member_info['member_name'];
		$member_info['id'] = $this -> member_info['member_id'];
		$member_info['avator'] = getMemberAvatarForID($this -> member_info['member_id']);
		$member_info['point'] = $this -> member_info['member_points'];
		$member_info['predepoit'] = $this -> member_info['available_predeposit'];
		$member_info['available_rc_balance'] = $this -> member_info['available_rc_balance'];
		//读取会员等级信息所有
		//$model_member = Model('member');
		//$member_grade = $model_member->getOneMemberGrade(intval($this->member_info['member_id']));
		//exit(print_r($member_info));
		//$member_info['level_name'] = $this->member_info['level_name'];
		$favorites_model = Model('favorites');
		$member_info['favorites_store'] = $favorites_model -> getStoreFavoritesCountByStoreId('', $this -> member_info['member_id']);
		//店铺收藏数
		$member_info['favorites_goods'] = $favorites_model -> getGoodsFavoritesCountByGoodsId('', $this -> member_info['member_id']);
		//商品收藏数
		//生成二维码　
		include "../phpqrcode/phpqrcode.php";
		$value = "http://localhost/wap/tmpl/member/register.html?rec=" . $member_info['id'];
		$errorCorrectionLevel = "L";
		$matrixPointSize = "4";
		$path = '../phpqrcode/codes/';
		$fileName = $path . 'code_' . $member_info['id'] . '.png';
		QRcode::png($value, $fileName, $errorCorrectionLevel, $matrixPointSize);
		$username = $_GET['username'];
		//上一天
		$nowday = date("Y-m-d 23:59:59", strtotime("-1 day"));
		$ytoday = date("Y-m-d", strtotime("-1 day"));
		$today = date("Y-m-d");
		$yestoday = date("Y-m-d", strtotime("-1 day"));
		$nowday = strtotime($nowday);
		$ytoday = strtotime($ytoday);
		//backpoints
//会员升级宝豆upgradepoints
$sql_up="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='upgradepoints' and pl_addtime between ".$ytoday." and ".$nowday."";
$res_up=mysql_query($sql_up);
$row_up=mysql_fetch_array($res_up);
//门店消费豆storepoints
$sql_st="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='storepoints' and pl_addtime between ".$ytoday." and ".$nowday."";
$res_st=mysql_query($sql_st);
$row_st=mysql_fetch_array($res_st);
//返还宝豆（暂停，目前返还到分红豆）
//$sql_bc="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='backpoints' and pl_addtime between ".$ytoday." and ".$nowday."";
//$res_bc=mysql_query($sql_bc);
//$row_bc=mysql_fetch_array($res_bc);
//在线订购
$sql_order="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='order' and pl_addtime between ".$ytoday." and ".$nowday."";
$res_order=mysql_query($sql_order);
$row_order=mysql_fetch_array($res_order);
//登录赠送
$sql_lo="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='login' and pl_addtime between ".$ytoday." and ".$nowday."";
$res_lo=mysql_query($sql_lo);
$row_lo=mysql_fetch_array($res_lo);
//注册赠送
$sql_re="select sum(pl_points) as ypoints from shopwwi_points_log where  pl_stage='regist' and pl_addtime between ".$ytoday." and ".$nowday."";
$res_re=mysql_query($sql_re);
$row_re=mysql_fetch_array($res_re);
$allpoints=$row_up['ypoints']+$row_st['ypoints']*1.16+$sql_order['ypoints']*1.16+$sql_lo['ypoints']+$sql_re['ypoints'];
		
		$member_info['bjf'] = number_format($allpoints,2);
		//echo "<dl class='line-right'><span>上一天新增白积分总量：</span><dd>".number_format($allpoints,2)."</dd></dl>";
		$sql = "select sum(pl_points) as rpoints from shopwwi_points_log where pl_stage='turnpoints' and pl_addtime between " . $ytoday . " and " . $nowday . "";
		//echo $sql;
		$res = mysql_query($sql);
		$row = mysql_fetch_array($res);
		$rpoints = abs($row['rpoints']);
		if ($rpoints == "") {
			$rpoints = 0;
		}
		$member_info['fhjf'] = number_format($rpoints, 2);
		//echo "<dl class='line-right'><span>上一天返还积分总量：</span><dd>".number_format($rpoints,2)."</dd></dl>";
		//上一天每单元返还积分数
		//$sql="select * from shopwwi_returnpoint where addtime = '".$yestoday."'";
		$sql = "select * from shopwwi_returnpoint order by id desc";
		$res_sr = mysql_query($sql);
		$row_sr = mysql_fetch_array($res_sr);
		$back_point = $row_sr['back_point'];
		if ($back_point == "") {
			$back_point = 0;
		}
		$member_info['qfdy'] = round($rpoints / $back_point);
		//echo "<dl class='line-right'><span>上一天全返单元总量：</span><dd>".round($rpoints/$back_point)."</dd></dl>";
		$member_info['dyjf'] = number_format($back_point, 4);
		//echo "<dl><span>上一天每单元返还积分数：</span><dd>".number_format($back_point,4)."</dd></dl>";
		output_data(array('member_info' => $member_info));
	}
	/**
	 * 我的积分
	 */
	public function my_assetOp() {
		$member_info = $this -> getMemberAndGradeInfo(true);
		$point = $this -> member_info['member_points'];
		$reds_point = $this -> member_info['member_red_points'];
		$kucun_point = $this -> member_info['kucun_points'];
		$exppoints = $member_info['member_exppoints'];
		$predepoit = $this -> member_info['available_predeposit'];
		$balance = $this -> member_info['available_rc_balance'];
		$voucher = Model('voucher') -> getCurrentAvailableVoucherCount($this -> member_info['member_id']);
		//取得当前有效代金券数量
		$redpacket = Model('redpacket') -> getCurrentAvailableRedpacketCount($this -> member_info['member_id']);
		//取得当前有效红包数量
		if ($_GET["fields"] == 'predepoit') {
			output_data(array('predepoit' => $predepoit));
		} elseif ($_GET["fields"] == 'available_rc_balance') {
			output_data(array('available_rc_balance' => $balance));
		} else {
			$model_member = Model('member');
			$member_infotmp = $model_member -> getMemberInfoByID($this -> member_info['member_id']);
			$member_infotmp['member_exppoints'] = intval($member_infotmp['member_exppoints']);
			//当前登录会员等级信息
			$membergrade_info = $model_member -> getOneMemberGrade($member_infotmp['member_exppoints'], true);
			$member_info2 = array_merge($member_infotmp, $membergrade_info);
			$membergrade_arr = $model_member -> getMemberGradeArr(true, $member_infotmp['member_exppoints'], $member_infotmp['level']);
			
			 $list_setting = Model('setting')->getListSetting();//site_shuifei
			output_data(array('point' => $point, 'reds_point' => $reds_point, 'kucun_point' => $kucun_point, 'exppoints' => $exppoints, 'predepoit' => $predepoit, 'available_rc_balance' => $balance, 'redpacket' => $redpacket, 'voucher' => $voucher, 'member_info' => $member_info2, 'membergrade_arr' => $membergrade_arr, 'site_shuifei' => $list_setting['site_shuifei']));
		}
	}
	protected function getMemberAndGradeInfo($is_return = false) {
		$member_info = array();
		//会员详情及会员级别处理
		if ($this -> member_info['member_id']) {
			$model_member = Model('member');
			$member_info = $model_member -> getMemberInfoByID($this -> member_info['member_id']);
			if ($member_info) {
				$member_gradeinfo = $model_member -> getOneMemberGrade(intval($member_info['member_exppoints']));
				$member_info = array_merge($member_info, $member_gradeinfo);
				$member_info['security_level'] = $model_member -> getMemberSecurityLevel($member_info);
			}
		}
		if ($is_return == true) {//返回会员信息
			return $member_info;
		} else {//输出会员信息
			Tpl::output('member_info', $member_info);
		}
	}
}